<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="合伙人名称">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="合伙人手机号">
                <a-input v-model="queryParam.phone" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="合伙人等级">
                <a-select v-model="queryParam.level" placeholder="请选择" default-value="">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option value="1">有限合伙人</a-select-option>
                  <a-select-option value="2">邀约合伙人</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="晋升时间">
                <a-range-picker :allowClear="true" ref="createdAtRangePicker" @change="createdAtRangePickerOnChange" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="销售时段">
                <a-range-picker
                  :allowClear="true"
                  ref="performanceRangePicker"
                  @change="performanceRangePickerOnChange"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                <a-button style="margin-left: 8px" @click="handleExport">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="() => { this.formVisible = true }">代理升合伙人</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{ x: 1300 }"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a-popconfirm placement="top" @confirm="confirmDelete(record)">
              <template slot="title">
                确定要删除吗?
              </template>
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>

    <partners-form
      :visible="formVisible"
      @visible="val => this.formVisible = val"
      @refreshList="() => this.$refs.table.refresh()"
    />
  </div>
</template>

<script>
  import { STable } from '@/components'
  import { getPartnerList, deletePartner } from '@/api/proxy'
  import PartnersForm from '@/views/proxy/partner/Form'
  import { formatAmount } from '@/utils/util'

  const LEVEL_MAP = {
    1: '有限合伙人',
    2: '邀约合伙人'
  }

  export default {
    name: 'List',
    components: {
      PartnersForm,
      STable
    },
    data () {
      return {
        formVisible: false,
        mdl: {},
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'id'
          },
          {
            title: '合伙人名称',
            dataIndex: 'name'
          },
          {
            title: '合伙人手机号',
            dataIndex: 'phone'
          },
          {
            title: '合伙人等级',
            dataIndex: 'partner_identity.level',
            customRender: level => LEVEL_MAP[level]
          },
          {
            title: '合伙人业绩',
            customRender: record => formatAmount(record.hasOwnProperty('performance') ? (record.performance || 0) : (record.partner_identity.performance || 0))
          },
          {
            title: '佣金',
            customRender: record => formatAmount(record.hasOwnProperty('commission') ? (record.commission || 0) : (record.partner_identity.commission || 0))
          },
          {
            title: '奖励',
            customRender: record => formatAmount(record.hasOwnProperty('reward') ? (record.reward || 0) : (record.partner_identity.reward || 0))
          },
          {
            title: '晋升时间',
            dataIndex: 'partner_identity.created_at'
          },
          {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getPartnerList(Object.assign(parameter, this.queryParam))
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {

    },
    methods: {
      confirmDelete (record) {
        deletePartner(record.partner_identity.id).then(() => this.$refs.table.refresh())
      },
      createdAtRangePickerOnChange (date, dateString) {
        this.queryParam.created_at_begin = dateString[0]
        this.queryParam.created_at_end = dateString[1]
      },
      performanceRangePickerOnChange (date, dateString) {
        this.queryParam.performance_range_begin = dateString[0]
        this.queryParam.performance_range_end = dateString[1]
      },
      handleReset () {
        this.queryParam = {}
        this.$refs.createdAtRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
        this.$refs.performanceRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      },
      handleExport () {
        getPartnerList(Object.assign({ export: true }, this.queryParam))
      }
    }
  }
</script>
